// pages/index/index.js
import {
  stats,
  todoList,
  noticeList
} from '../../api.js'
var app = getApp(); //获取App内部的信息
Page({
  data: {
    count:0,
    count1:0,
    info:[],
    totalIncome:5,
    totalReceive:5,
    storageProductsNumber:5,
    storageProductMoney:5,
    updateTime:'',
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  wx.showLoading({
    title: '加载中',
  })
  },
  onShow:function(){
    var that = this
    todoList({ token: app.globalData.token }).then(res => {
      if(res.data.code==666){
        let info = res.data.data;
        console.log('length长度A', res.data.data.length)
        that.setData({
          count: info.length,
          info: res.data.data
        })        
      }else if (res.data.code == 111111) {
        wx.showToast({
          title: '登录失效，请重新登录',
          icon: 'none',
        })
        setTimeout(function () {
          wx.reLaunch({
            url: '/pages/user/login/login',
          })
        }, 1500)
      }else{
        wx.showToast({
          title: res.data.msg,
          icon:'none'
        })
      }
    })
    let _this = this;
    console.log(app.globalData.token)
    stats({ token: app.globalData.token }).then(({ data }) => {
      console.log('数据接口', data)
      if(data.code==666){
        _this.setData({
          totalIncome: data.data.generalBusiness.totalIncome,
          totalReceive: data.data.generalBusiness.totalReceive,
          storageProductsNumber: data.data.generalBusiness.storageProductsNumber,
          storageProductMoney: data.data.generalBusiness.storageProductMoney,
        })
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
  
    })
    let now = new Date();
    let myTime = now.toLocaleDateString();

    // let month = now.getMonth() - 0 <11?'0'+now.getMonth() - 0 + 1 : now.getMonth();
    // let date=now.getDate();
    // myTime = `${month} - ${date}`;
    // console.log(`${month}-${date}`)
    this.setData({
      updateTime: myTime
    })
    noticeList({ token: app.globalData.token, page: 1, singlepage: 20, is_read: 0 }).then(res => {
      if(res.data.code==666){
        this.setData({
          count1: res.data.data.total
        })
      } else if (res.data.code == 111111) {
        wx.showToast({
          title: '登录失效，请重新登录',
          icon: 'none',
        })
        setTimeout(function () {
          wx.reLaunch({
            url: '/pages/user/login/login',
          })
        }, 1500)
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
    })
    wx.hideLoading();
  },
  skipControl(){
    wx.setStorageSync('controlType','goodControl')
    wx.reLaunch({
      url: '/pages/goodControl/goodControl',
    })
  },
  skipAllControl(){
    wx.setStorageSync('controlType', 'allGoodControl')
    wx.reLaunch({
      url: '/pages/fullStock/fullStock',
    })
  },
  skipOnlineOrder(){
    wx.reLaunch({
      url: '/pages/orderControl/orderControl',
    })
  },
  todo(e){
    console.log(e.currentTarget.dataset.id,e)
    let id = e.currentTarget.dataset.id;
    let info_batchcode = e.currentTarget.dataset.infobatchcode
    wx.navigateTo({
      url: '/pages/orderControl/orderDetail/orderDetail?type=todo&id='+id+'&info_batchcode='+info_batchcode,
    })
  },
  toWait(){
    wx.navigateTo({
      url: '/pages/index/todoList/todoList',
    })
  },
  onShareAppMessage:function(){
    return {
      title: '赋能奢商，只为业绩增长',
      imageUrl: "https://document.growthupup.com/growthShare.jpg?t=" + new Date().getTime(),
      path: '/pages/user/login/login',
    }
  },
  onTabItemTap(item) {
    console.log('item.index', item.index)
    console.log('item.pagePath', item.pagePath)
    console.log('item.text',item.text)
  }
})